package com.lej.home.pay

import com.alibaba.android.arouter.facade.annotation.Route
import com.lej.core.base.BaseActivity
import com.lej.core.common.IS_ORDER_DETAIL
import com.lej.core.common.ORDER_DETAIL_ID
import com.lej.core.dsl.src
import com.lej.core.router.CRouter
import com.lej.core.utils.extention.formatTimeBySecond
import com.lej.core.utils.extention.toast
import com.lej.core.utils.load
import com.lej.dataservice.club.bean.main.home.BuyDetailInfo
import com.lej.dataservice.club.bean.main.home.OrderDetailInfo
import com.lej.dataservice.club.http.bean.onError
import com.lej.dataservice.club.http.bean.onSuccess
import com.lej.home.BR
import com.lej.home.R
import com.lej.home.databinding.HomeActivityPayResultBinding
import com.qmuiteam.qmui.kotlin.onClick

/**
 *  author: cjq
 *  date  : 2022/7/18 0018
 *  desc  :
 */
@Route(path = CRouter.nftPayResultDetail)
class HomeNftPayResultActivity : BaseActivity<HomeActivityPayResultBinding, HomePayResultVM>() {
    override fun getLayout(): Int = R.layout.home_activity_pay_result

    override fun initVariableId(): Int = BR.vm

    override fun initData() {
        super.initData()
        val type = intent.getBooleanExtra(IS_ORDER_DETAIL, false)
        if (type) {
            viewModel.orderId = intent.getIntExtra(ORDER_DETAIL_ID, -1)
            getOrderDetail()
        } else {
            viewModel.buyId = intent.getIntExtra(ORDER_DETAIL_ID, -1)
            getBuyDetail()
        }
        binding.ivBack.onClick { finish() }
        binding.ivBuyState.src =
            if (type) R.drawable.ic_pay_result_wait else R.drawable.ic_pay_result_finish
    }

    /**
     * 获取购买订单详情
     */
    private fun getBuyDetail() {
        viewModel.buyOrderDetail().onSuccess(this) {
            setData(it)
        }.onError(this) {
            it.message.toast()
        }
    }

    /**
     * 获取求购订单详情
     */
    private fun getOrderDetail() {
        viewModel.orderDetail().onSuccess(this) {
            setData(it)
        }.onError(this) {
            it.message.toast()
        }
    }

    private fun setData(info: BuyDetailInfo) {
        load(info.cover, R.drawable.ic_load_place, R.drawable.ic_load_place_error, binding.ivCover)
        binding.tvTitle.text = info.name ?: ""
        binding.tvAuthor.text = "创作者·${info.nick ?: ""}"
        binding.tvStyle.text = "数字文创·${info.getNftType()}"
        binding.tvPrice.text = "原价·¥ ${(info.per_fee ?: 0) / 100f}"
        binding.tvSerialNum.text = info.order_no ?: ""
        binding.tvBuyNum.text = "${info.unit ?: 0}"
        binding.tvTotalNum.text = "¥${(info.fee ?: 0) / 100f}"
        val timeStr = (info.order_time ?: 0L).formatTimeBySecond("MM-dd HH:mm")
        binding.tvTime.text = "（下单时间  $timeStr）"
        binding.tvStatus.text = info.getStateStr()
    }

    private fun setData(info: OrderDetailInfo) {
        load(info.cover, R.drawable.ic_load_place, R.drawable.ic_load_place_error, binding.ivCover)
        binding.tvTitle.text = info.nft_name ?: ""
        binding.tvAuthor.text = "创作者·${info.nft_author ?: ""}"
        binding.tvStyle.text = "数字文创·${info.getNftType()}"
        binding.tvPrice.text = "原价·¥ ${(info.price ?: 0) / 100f}"
        binding.tvSerialNum.text = info.order_no ?: ""
        binding.tvBuyNum.text = "${info.quantity ?: 0}"
        binding.tvTotalNum.text = "¥${(info.fee ?: 0) / 100f}"
        val timeStr = (info.time_timestamp ?: 0L).formatTimeBySecond("MM-dd HH:mm")
        binding.tvTime.text = "（下单时间  $timeStr）"
        binding.tvStatus.text = info.getStateStr()
    }

}